<?php

namespace App\Http\Controllers\Wap;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Model\Card;
use App\Model\Member;
use App\Model\Picture;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class CardController extends Controller
{
    public function __construct(Request $request){
        $this->request = $request;
//        $this->openid = session('wechat_user')['id'];
        $this->openid = 'oiYTrwfH-VLzRn8OkLsn_knKv3qM';
        if (!empty($this->openid)) {
            $this->memberId = Member::where('openid',$this->openid)->pluck('id');
        }
    }

    /**
     * 实体卡激活界面
     * czs
     */
    public function cardActive(){
        return view('wap.card.cardActive');
    }

    public function card(){
        return view('wap.card.index');
    }

    /**
     * @return mixed
     * 实体卡激活
     * czs
     */
    public function cardResult(){
        $cardNum = $this->request->input('cardNum');
        $password = $this->request->input('password');
        $res = Card::cardActive($cardNum,$password,$this->memberId)->getData();
        return view('wap.card.cardResult')
            ->with('t',!empty($res['t'])?$res['t']:"")
            ->with('f',!empty($res['f'])?$res['f']:"");
    }

    /**
     * 电子码上传头像和列表
     * czs
     */
    public function dzm(){
        if($this->request->isMethod('post')) {
            $cardNum = $this->request->input('cardNum');
            $password = $this->request->input('password');
            $res = Card::bindMan($cardNum,$password,$this->memberId);
            return $res->getResult();
        } else {
            $res = Card::ownEcard($this->memberId,$limit=30)->getData();
            return view('wap.card.dzm')
                ->with('res',$res);
        }
    }

    /**
     * 已激活电子卡列表
     * czs
     */
    public function ecard(){
        $res = Card::activeEcard($this->memberId,$limit=30)->getData();
        return view('wap.card.ecard')
            ->with('res',$res);
    }

    public function ecardUpload() {
        $id = $this->request->input('cardId');
        $lock = Card::where('id',$id)->pluck('lock');
        if ($lock) {
            return [
              'success'=>false,
              'msg' => "头像已锁定，无法更换！"
            ];
        }
        $file = $this->request->file('photo');
        $destinationPath = 'temp/';
        try {
            $exif = exif_read_data($file->getPath().DIRECTORY_SEPARATOR.$file->getFilename());
        }
        catch (\Exception $e) {
            $exif = false;
        }
//        $exif = exif_read_data($file->getPath().DIRECTORY_SEPARATOR.$file->getFilename());
        $contents = file_get_contents($file->getPath().DIRECTORY_SEPARATOR.$file->getFilename());
        $fileName = time().'_'.substr(md5($file->getClientOriginalName()),0,10).'.'.$file->getClientOriginalExtension();
        $filePath = $destinationPath.$fileName;
        Storage::put($filePath,$contents);
        if ($exif){
        } else {
            $img = imagecreatefromjpeg("uploads/pic/".$filePath);
            $rotate = imagerotate($img,-90,0);
            imagejpeg($rotate,"uploads/pic/".$filePath);
        }
//        if(!empty($exif['Orientation'])) {
//            $img = imagecreatefromjpeg("uploads/pic/".$filePath);
//            switch($exif['Orientation']) {
//                case 8:
//                    $rotate = imagerotate(imagecreatefromstring($img),90,0);
//                    break;
//                case 3:
//                    $rotate = imagerotate($img,180,0);
//                    break;
//                case 6:
//                    $rotate = imagerotate($img,-90,0);
//                    break;
//            }
//            $rotate = imagerotate($img,-90,0);
//            imagejpeg($rotate,"uploads/pic/".$filePath);
//        }
        $single = Picture::makeThumbAndCreate($fileName);
        $data['img'] = $single->getData();
        $res = Card::where('id',$id)->update($data);
        if ($res) {
            DB::table('members')->where('id',$this->memberId)->update(['vip'=>1]);
            $return = array(
                'success' => true,
                'msg' => "上传成功！"
            );
        } else {
            $return = array(
                'success' => false,
                'msg' => "上传失败，请重试！"
            );
        }
        return $return;
    }

    /**
     * 电子卡二维码
     * czs
     */
    public function qrcode($id){
        $res = Card::getCard($id)->getData();
        return view('wap.card.qrcode')
            ->with('res',$res);
    }

    /**
     *
     * 上传头像
     * czs
     */
    public function uploadImg() {
        $file = $this->request->file('head');
        $id = $this->request->input('cardId');
        $status = Card::where('id',$id)->pluck('status');
        if ($status==2) {
            return redirect('wap/card/dzm')->with('status2', '2');
        }
        $destinationPath = 'temp/';
        try {
            $exif = exif_read_data($file->getPath().DIRECTORY_SEPARATOR.$file->getFilename());
        }
        catch (\Exception $e) {
            $exif = false;
        }
        $contents = file_get_contents($file->getPath().DIRECTORY_SEPARATOR.$file->getFilename());
        $fileName = time().'_'.substr(md5($file->getClientOriginalName()),0,10).'.'.$file->getClientOriginalExtension();
        $filePath = $destinationPath.$fileName;
        Storage::put($filePath,$contents);
        if ($exif){
        } else {
            $img = imagecreatefromjpeg("uploads/pic/".$filePath);
            $rotate = imagerotate($img,-90,0);
            imagejpeg($rotate,"uploads/pic/".$filePath);
        }
        $single = Picture::makeThumbAndCreate($fileName);
        $data['img'] = $single->getData();
        $data['status'] = 2;
        $data['member_id'] = $this->memberId;
        $res = Card::where('id',$id)->update($data);
        if ($res) {
            DB::table('members')->where('id',$data['member_id'])->update(['vip'=>1]);
            return redirect('wap/card/ecard');
        }
    }

    /**
     * 锁定头像
     * czs
     */
    public function lock()
    {
        if ($this->request->isMethod('post')) {
            $id = $this->request->input('id');
            $res = Card::where('id', $id)->update(["lock" => 1]);
            if ($res) {
                $return = array(
                    'success' => true,
                    'msg' => "操作成功！"
                );
            } else {
                $return = array(
                    'success' => false,
                    'msg' => "操作失败，请重试！"
                );
            }
            return $return;
        } else {
            $id = $this->request->get('id');
            $lock = Card::where('id',$id)->pluck('lock');
            return [
               'success' => true,
               'data' => $lock
            ];
        }
    }
}
